<template>
  <div class="app-container">
    <div style="margin-bottom: 20px;">
      <el-button @click="onLaunchAllTap" v-loading="isLoading">一键启动全部游戏</el-button>
    </div>
    <el-table :data="projectsList" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="Index" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column prop="id" label="ID" width="95">
      </el-table-column>
      <el-table-column prop="icon" align="center" label="ICON" width="95">
        <template slot-scope="scope">
          <i :class="scope.row.icon" style="width: 32px; height: 32px; background-size: 32px"></i>
        </template>
      </el-table-column>
      <el-table-column prop="bot" label="项目">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="200">
        <template slot-scope="scope">
          {{ isLoading ? '执行中' : '空闲' }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="onInviteSetClick(scope.row)" type="text" size="small">设置邀请</el-button>
          <el-button @click="onPreviewClick(scope.row)" type="text" size="small">预览</el-button>
        </template>
      </el-table-column>
    </el-table>
    <add-invites :visible="this.showProxyDialog" :project="this.currentProject"
      @on-dismiss="onDialogDismiss"></add-invites>
  </div>
</template>

<script>

import WorkerManager from '@/ipc/worker_manager';
import AddInvites from './components/AddInvites.vue';

export default {
  components: {
    AddInvites,
  },
  data() {
    return {
      isLoading: false,
      showProxyDialog: false,
      currentProject: null
    }
  },
  computed: {
    projectsList() {
      let projectsList = this.$store.state.projects.projectsList;
      (projectsList ?? []).sort((e1, e2) => {
        return e2.id > e1.id ? 1 : -1;
      })
      return projectsList;
    },
  },
  mounted() {
    this.isLoading = WorkerManager.isAllGameRunning()
    this.$store.dispatch("projects/loadProjects")
  },
  methods: {
    onLaunchAllTap() {
      this.isLoading = true
      WorkerManager.launchAllGame()
      this.$message({
        type: "success",
        message: "一键启动成功，请关注游戏的更新时间和游戏次数",
      })
    },
    async onLaunchClick(project) {
      if (project.project == "hamster_kombat_bot") {
        await WorkerManager.shared().launchGame("hamster")
      }
      if (project.project == "PiggyPiggyofficialbot") {
        await PiggyManager.shared().launch()
      }
      if (project.project == "BlumCryptoBot") {
        await BlumManager.shared().launch()
      }
      if (project.project == "tonstationgames_bot") {
        await TonstationManager.shared().launch()
      }
      if (project.project == "DuckChain_bot") {
        await DuckchainManager.shared().launch()
      }
      if (project.project == "TimeFarmCryptoBot") {
        await TimefarmManager.shared().launch()
      }
      if (project.project == "VanaDataHeroBot") {
        await VanaManager.shared().launch()
      }
      this.isLoading = true
      setTimeout(() => {
        this.isLoading = false
      }, 60 * 1000);
    },
    onStopClick(account) {
      console.log(account)
    },
    onPreviewClick(project) {
      this.$router.push({
        path: project.route
      })
    },
    onInviteSetClick(project) {
      this.currentProject = project
      console.log(project)
      this.showProxyDialog = true
    },
    onDialogDismiss(textContent) {
      if (textContent == null) {
        this.showProxyDialog = false
        this.currentProject = null
        return
      }
      let inviteList = textContent.split("\n")
      inviteList = inviteList.filter(item => item.startsWith("https://t.me/"))
      console.log(inviteList)
      this.currentProject.inviteList = inviteList
      this.$store.dispatch("projects/updateProject", this.currentProject)
      this.showProxyDialog = false
      this.currentProject = null
    }
  }
}
</script>